package com.atguigu.ssyx.common.result;

import lombok.Data;

@Data
public class Result<T> {

    //    状态码
    private Integer code;

//    消息

    private String message;
//    data数据

    //    使用object,还需要转类型,所以使用object.所以使用T泛型..
    private T data;


    private Result() {
    }


    //返回数据对象
    public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
        Result result = new Result();
        if (data != null) {
            result.setData(data);
        }
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public static <T> Result<T> build(T data, String message, Integer code) {
        Result result = new Result();
        if (data != null) {
            result.setData(data);
        }

        result.setCode(code);
        result.setMessage(message);
        return result;
    }


    //    返回成功
    public static <T> Result<T> ok(T data) {
        return Result.build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok() {
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //    返回失败
    public static <T> Result<T> fail(T data) {
        return Result.build(data, ResultCodeEnum.FAIL);
    }

    //使用fail和ok都是状态码和消息是固定的.
    //ResultCodeEnum resultCodeEnum=ResultCodeEnum.SUCCESS


}
